//
//  ControlTemplateViewProtocolController.m
//  Uhomebk
//
//  Created by 揭婧雯 on 17/3/3.
//  Copyright © 2017年 com.segi. All rights reserved.
//

#import "ControlTemplateViewProtocolController.h"

#pragma mark -
#pragma mark 协议选择器【protocol】
//#import "BaseWebViewController.h"

@interface ControlTemplateViewProtocolController ()
@property (nonatomic, strong) NSString* defaultValue;
@end

@implementation ControlTemplateViewProtocolController

static ControlTemplateViewProtocolController* sharedManagerProtocolInter = nil;

+ (ControlTemplateViewProtocolController*)sharedManagerProtocol
{
    sharedManagerProtocolInter = [[self alloc] init];
    
    return sharedManagerProtocolInter;
}

- (void)initVar
{
    _valueStr = @"";
    _param_id = [SEGUtils ValueToString:[_params valueForKey:@"attrInstId"]];
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initVar];
    self.isNeedAdpterHotspot = YES;
    
    _bgMainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 50)];
    
    _bgMainView.backgroundColor = kColor_W;
    self.tempBgView = _bgMainView;
    
    [self.view addSubview:_bgMainView];
    
    _paramId = [_params valueForKey:@"attrInstId"];
    
    ////////判断defaultValue是否有值
    if ([[_params valueForKey:@"initData"] length] > 0) {
        _defaultValue = [_params valueForKey:@"initData"];
    }
    
    ////////是否可读 0-不显示 1-显示
    if ([[SEGUtils ValueToString:[_params valueForKey:@"readable"]] isEqualToString:@"0"]) {
        _bgMainView.frame = CGRectMake(0, 0, CGRectGetWidth(_bgMainView.bounds), 0);
    }
    else {
        //线条
        UIView* lineView = [[UIView alloc] initWithFrame:CGRectMake(15, CGRectGetHeight(_bgMainView.bounds) - 0.5, CGRectGetWidth(_bgMainView.bounds), 0.5)];
        lineView.backgroundColor = kColor_L2;
        lineView.tag = 8888;
        [_bgMainView addSubview:lineView];
    }
    
    ////////是否必填 //是否可读 1-必须 0-可不填
    _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
    _valueStr = [_params valueForKey:@"attrValue"];
    ////////是否可写 //是否可读 0-不可写 1-可写
    if ([[SEGUtils ValueToString:[_params valueForKey:@"writable"]] isEqualToString:@"0"]) //当writable为0时可读
    {
        //标题
        UILabel* titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(HEADSPACE, 0, CGRectGetWidth(_bgMainView.bounds), 50)];
        [titleLabel setText:[NSString stringWithFormat:@"同意《%@》", [_params valueForKey:@"title"]]];
        [titleLabel setTextColor:kColor_1];
        [titleLabel setFont:kFont_16];
        [_bgMainView addSubview:titleLabel];
    }
    else {
        //按钮
        UIImage* image = _valueStr.intValue == 0?[UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_select_nor")]:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_select")];
        _isAccept = _valueStr.boolValue;
        CustomButtonView* abtn = [[CustomButtonView alloc] initWithFrame:CGRectMake(HEADSPACE, 0, CGRectGetWidth(_bgMainView.bounds) - 20, 50)];
        [abtn addTarget:self action:@selector(onBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_bgMainView addSubview:abtn];
        
        //图标
        UIImageView* iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, (CGRectGetHeight(abtn.bounds) - image.size.height) / 2, image.size.width, image.size.height)];
        [iconImageView setImage:image];
        [abtn addSubview:iconImageView];
        
        //标题
        
        UILabel* titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(image.size.width + HEADSPACE, 0, CGRectGetWidth(abtn.bounds), 50)];
        //        [titleLabel setText:[NSString stringWithFormat:@"同意《%@》",[_params valueForKey:@"title"]]];
        [titleLabel setTextColor:kColor_1];
        [titleLabel setFont:kFont_16];
        [abtn addSubview:titleLabel];
        
        NSString* valueStr = [NSString stringWithFormat:@"同意《%@》", [_params valueForKey:@"title"]];
        NSMutableAttributedString* str = [[NSMutableAttributedString alloc] initWithString:valueStr];
        [str addAttribute:NSForegroundColorAttributeName value:kColor_T range:NSMakeRange(2, [valueStr length] - 2)];
        titleLabel.attributedText = str;
        
        CustomButtonView* bbtn = [[CustomButtonView alloc] initWithFrame:CGRectMake(40, 0, CGRectGetWidth(_bgMainView.bounds) -2*HEADSPACE, 50)];
        [bbtn addTarget:self action:@selector(onBtnOpenWebAction:) forControlEvents:UIControlEventTouchUpInside];
        [_bgMainView addSubview:bbtn];
    }
}

- (void)onBtnOpenWebAction:(CustomButtonView*)sender
{
    NSDictionary * urldic = [SEGUtils dictionaryWithJsonString:[_params valueForKey:@"initData"]];
    UIViewController *webVc = [[SEGUhomeMediator sharedInstance] seg_initWebViewLoadController:@{@"linkURL":[urldic valueForKey:@"url"]}];
    webVc.title = [_params valueForKey:@"title"];
    [self.masterVC.navigationController pushViewController:webVc animated:YES];
}

- (void)onBtnAction:(CustomButtonView*)sender
{
    if (_isAccept) {
        _isAccept = NO;
        UIImage* image = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_select_nor")];
        UIImageView* aimageView = [sender.subviews objectAtIndex:0];
        [aimageView setImage:image];
        
        _valueStr = @"0";
        _optional = [SEGUtils ValueToString:[_params valueForKey:@"required"]];
    }
    else {
        _isAccept = YES;
        UIImage* image = [UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_select")];
        UIImageView* aimageView = [sender.subviews objectAtIndex:0];
        [aimageView setImage:image];
        
        _valueStr = @"1";
        _optional = @"0";
    }
}

/**
 *  给协议控件填充值
 *
 *  @param valueStr 接收字符串值
 */
- (void)paddingProtocolView:(NSString*)valueStr
{
    _valueStr = valueStr;
    if ([_valueStr isEqualToString:@"1"]) {
        for (CustomButtonView* abtn in _bgMainView.subviews) {
            if ([abtn isKindOfClass:[CustomButtonView class]]) {
                UIImageView* aimageView = [abtn.subviews objectAtIndex:0];
                [aimageView setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_select")]];
            }
        }
        _optional = @"0";
    }
    else {
        _optional = @"1";
    }
}

@end

